<?php
    class AdminRoleDAL{
        //获取角色列表
        public function getRoleList(){
            $query="select level,name from tb_level_list";

            $pdo=myPDO::getInstance();
            $statement=$pdo->prepare($query,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
            if($statement->execute()){
                $row=$statement->fetchAll(PDO::FETCH_ASSOC);
            }

            $statement->closeCursor();
            //$pdo=null;
            return $row;
        }

        //获取部门列表
        public function getDepartmentList(){
            $query="select department_id,department_name from tb_department_list";

            $pdo=myPDO::getInstance();
            $statement=$pdo->prepare($query,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
            if($statement->execute()){
                $row=$statement->fetchAll(PDO::FETCH_ASSOC);
            }

            $statement->closeCursor();
            //$pdo=null;
            return $row;
        }

        //添加用户
        public function addUser($userInfo){
            $executeReturn=false;
            $departments=$userInfo->department;
            $pdo=myPDO::getInstance();

            try{
                $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
                $pdo->beginTransaction();

                $insert_account="insert into tb_account(uid,password,register_time,level,status) values(:uid,:password,now(),:level,1)";
                $statement=$pdo->prepare($insert_account,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                $statement->bindValue(":uid",$userInfo->uid);
                $statement->bindValue(":password",$userInfo->password);
                $statement->bindValue(":level",$userInfo->role);
                $statement->execute();

                $insert_account_info="insert into tb_account_info(uid,name,phone_number) values(:uid,:name,:phone_number)";
                $statement=$pdo->prepare($insert_account_info,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                $statement->bindValue(":uid",$userInfo->uid);
                $statement->bindValue(":name",$userInfo->name);
                $statement->bindValue(":phone_number",$userInfo->phone);
                $statement->execute();

                $insert_department="insert into tb_department_personnel(uid,department_id,register_time) values(:uid,:department_id,now())";
                $statement=$pdo->prepare($insert_department,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                foreach($departments as $department){
                    $statement->bindValue(":uid",$userInfo->uid);
                    $statement->bindValue(":department_id",$department);
                    $executeReturn=$statement->execute();
                }

                if($executeReturn){
                    $pdo->commit();
                }
            }catch(Exception $e){
                $pdo->rollBack();
                //echo "Failed: ".$e->getMessage();
            }

            return $executeReturn;
        }

        //判断用户是否存在
        public function isUser($uid){
            $result=null;
            $query="select count(1) as count from tb_account where uid=:uid;";
            $pdo=myPDO::getInstance();
            $statement=$pdo->prepare($query,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
            $statement->bindValue(":uid",$uid);
            if($statement->execute()){
                $result=$statement->fetch(PDO::FETCH_ASSOC);
            }

            return $result;
        }
        //获取用户数量
        public function getUserCount($role=0,$search='',$status=1){

            $query_all="select count(*) as count from tb_account where status=:status;";
            $query_search="select count(tb_account.*) as count from tb_account,tb_account_info where (tb_account.uid=:search or tb_account_info.name=:search) and status=:status;";
            
            //按角色查找
            $query_role="select count(*) as count from tb_account where level=:role and status=:status;";
            $query_role_searc="select count(*) as count from tb_account where level=:role and (tb_account.uid=:search or tb_account_info.name=:search) and status=:status;";
            $pdo=myPDO::getInstance();
            if($role===0){//查找全部角色
                if($search==''){
                    $statement=$pdo->prepare($query_all,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                    $statement->bindValue(":status",$status);
                }else{
                    $statement=$pdo->prepare($query_search,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                    $statement->bindValue(":search",$search);
                    $statement->bindValue(":status",$status);
                }
                
            }else{
                if($search==''){
                    $statement=$pdo->prepare($query_role,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                    $statement->bindValue(":role",$role);
                    $statement->bindValue(":status",$status);
                }else{
                    $statement=$pdo->prepare($query_role_searc,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                    $statement->bindValue(":search",$search);
                    $statement->bindValue(":role",$role);
                    $statement->bindValue(":status",$status);
                }
            }
            if($statement->execute()){
                $row=$statement->fetchAll(PDO::FETCH_ASSOC);
            }

            $statement->closeCursor();
            //$pdo=null;
            return $row;
        }
        //获取用户列表
        public function getUserList($role,$page=0,$pageSize=10,$search){
            $row=null;

            $query_all="select tb_account.uid,tb_level_list.name as role, tb_account_info.name,tb_account_info.phone_number as phone,tb_level_list.describe 
             from tb_account,tb_account_info,tb_level_list
             where tb_account.uid=tb_account_info.uid 
             and tb_account.level=tb_level_list.level 
             and tb_account.status=1 
             order by tb_account.uid LIMIT :page,:pageSize;";

            $query_role="select tb_account.uid,tb_level_list.name as role, tb_account_info.name,tb_account_info.phone_number as phone,tb_level_list.describe 
             from tb_account,tb_account_info,tb_level_list
             where tb_account.uid=tb_account_info.uid 
             and tb_account.level=:myRole and tb_account.level=tb_level_list.level 
             and tb_account.status=1 
             order by tb_account.uid LIMIT :page,:pageSize;";

             //手动条件查询
             $query_search="select tb_account.uid,tb_level_list.name as role, tb_account_info.name,tb_account_info.phone_number as phone,tb_level_list.describe 
             from tb_account,tb_account_info,tb_level_list
             where (tb_account.uid=:search_uid or tb_account_info.name=:search_name) 
             and tb_account.uid=tb_account_info.uid and tb_account.level=tb_level_list.level 
             and tb_account.status=1 
             order by tb_account.uid LIMIT :page,:pageSize;";

             $query_role_search="select tb_account.uid,tb_level_list.name as role, tb_account_info.name,tb_account_info.phone_number as phone,tb_level_list.describe 
             from tb_account,tb_account_info,tb_level_list
             where (tb_account.uid=:search_uid or tb_account_info.name=:search_name) 
             and tb_account.uid=tb_account_info.uid and tb_account.level=:myRole 
             and tb_account.level=tb_level_list.level 
             and tb_account.status=1 
             order by tb_account.uid LIMIT :page,:pageSize;";

            $pdo=myPDO::getInstance();
            if($search==''){
                if($role===0){
                    $statement=$pdo->prepare($query_all,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                    $statement->bindValue(":page",$page,PDO::PARAM_INT);
                    $statement->bindValue(":pageSize",$pageSize,PDO::PARAM_INT);
                    if($statement->execute()){
                        $row=$statement->fetchAll(PDO::FETCH_ASSOC);
                    }
                }else{
                    $statement=$pdo->prepare($query_role,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                    $statement->bindValue(":myRole",$role);
                    $statement->bindValue(":page",$page,PDO::PARAM_INT);
                    $statement->bindValue(":pageSize",$pageSize,PDO::PARAM_INT);
                    if($statement->execute()){
                        $row=$statement->fetchAll(PDO::FETCH_ASSOC);
                    }
                }
            }else{
                if($role===0){
                    $statement=$pdo->prepare($query_search,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                    $statement->bindValue(":search_uid",$search);
                    $statement->bindValue(":search_name",$search);
                    $statement->bindValue(":page",$page,PDO::PARAM_INT);
                    $statement->bindValue(":pageSize",$pageSize,PDO::PARAM_INT);
                    if($statement->execute()){
                        $row=$statement->fetchAll(PDO::FETCH_ASSOC);
                    }
                }else{
                    $statement=$pdo->prepare($query_role_search,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                    $statement->bindValue(":search_uid",$search);
                    $statement->bindValue(":search_name",$search);
                    $statement->bindValue(":myRole",$role);
                    $statement->bindValue(":page",$page,PDO::PARAM_INT);
                    $statement->bindValue(":pageSize",$pageSize,PDO::PARAM_INT);
                    if($statement->execute()){
                        $row=$statement->fetchAll(PDO::FETCH_ASSOC);
                    }
                }
            }

            $statement->closeCursor();
            //$pdo=null;
            return $row;
        }

        //获取用户所属部门
        public function getUserDepartment($uid){
            $row=null;

            $query="select tb_department_list.department_name from tb_department_list,tb_department_personnel 
            where tb_department_personnel.uid=:uid 
            and tb_department_personnel.department_id=tb_department_list.department_id 
            and tb_department_personnel.status!=0;";
            $pdo=myPDO::getInstance();
            $statement=$pdo->prepare($query,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
            $statement->bindValue(":uid",$uid);
            if($statement->execute()){
                $row=$statement->fetchAll(PDO::FETCH_ASSOC);
            }
            $statement->closeCursor();
            //$pdo=null;
            return $row;
        }

        //更新用户信息
        public function updateUserInformation($userInfo){
            $executeReturn=false;
            $pdo=myPDO::getInstance();

            $update_level="update tb_account set level=:level where uid=:uid;";
            $statement=$pdo->prepare($update_level,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
            $statement->bindValue(":level",$userInfo->role);
            $statement->bindValue(":uid",$userInfo->uid);
            $statement->execute();

            $update_info="update tb_account_info set name=:name,phone_number=:phone where uid=:uid;";
            $statement=$pdo->prepare($update_info,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
            $statement->bindValue(":name",$userInfo->name);
            $statement->bindValue(":phone",$userInfo->phone);
            $statement->bindValue(":uid",$userInfo->uid);
            $statement->execute();

            $update_department="update tb_department_personnel set status=0 where uid=:uid and status!=0;";
            $statement=$pdo->prepare($update_department,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
            $statement->bindValue(":uid",$userInfo->uid);
            $statement->execute();

            $insert_department="insert into tb_department_personnel(uid,department_id,register_time) values(:uid,:department_id,now());";
            foreach($userInfo->departments as $department){
                $statement=$pdo->prepare($insert_department,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                $statement->bindValue(":uid",$userInfo->uid);
                $statement->bindValue(":department_id",$department);
                $executeReturn=$statement->execute();
            }

            $statement->closeCursor();
            return $executeReturn;
        }

        //删除用户
        public function deleteUser($users,$status=0){
            $executeReturn=false;
            $pdo=myPDO::getInstance();
            try{
                $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
                $pdo->beginTransaction();

                foreach($users as $uid){
                    $query_account="update tb_account set tb_account.status=:status where uid=:uid;";
                    $statement=$pdo->prepare($query_account,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                    $statement->bindValue(":status",$status);
                    $statement->bindValue(":uid",$uid);
                    $statement->execute();

                    $query_account_info="update tb_account_info set tb_account_info.status=:status where uid=:uid;";
                    $statement=$pdo->prepare($query_account_info,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                    $statement->bindValue(":status",$status);
                    $statement->bindValue(":uid",$uid);
                    $statement->execute();

                    $query_department="update tb_department_personnel set tb_department_personnel.status=:status where uid=:uid;";
                    $statement=$pdo->prepare($query_department,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                    $statement->bindValue(":status",$status);
                    $statement->bindValue(":uid",$uid);
                    $executeReturn=$statement->execute();
                }

                if($executeReturn){
                    $pdo->commit();
                }
            }catch(Exception $e){
                $pdo->rollBack();
                //echo "Failed: ".$e->getMessage();
            }
            $statement->closeCursor();
            return $executeReturn;
        }
    }
?>